package com.qianya.util;

import com.qianya.model.pojo.patient.vo.CalendarVo;
import com.qianya.model.pojo.patient.vo.MonthVo;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**@Copyright CHJ
 * @Author HUANGP
 * @Date 2018年4月19日
 * @Desc 日期工具包
 */
public class DateUtils {
	
	public static final String df_Str = "yyyy-MM-dd HH:mm:ss";

	public static final String YYYY_MM_DD_Str = "yyyy-MM-dd";

	public static final String df_apm_Str = "yyyy-MM-dd ahh:mm:ss";
	/**
	 * 获取某个时间段内所有月份
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getMonthBetween(String startDate, String endDate){
		ArrayList<String> result = new ArrayList<String>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

			Calendar min = Calendar.getInstance();
			Calendar max = Calendar.getInstance();
			min.setTime(sdf.parse(startDate));
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

			max.setTime(sdf.parse(endDate));
			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

			Calendar curr = min;
			while (curr.before(max)) {
				result.add(sdf.format(curr.getTime()));
				curr.add(Calendar.MONTH, 1);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}
	/**
	 * 获取某个时间段内所有月份
	 * @return
	 * @throws ParseException
	 */
	public static List<MonthVo> listChangeMapReturn(List<MonthVo> list,String begin,String end){
		Map<String,String> map=new HashMap<String,String>();
		for (int i = 0; i <list.size() ; i++) {
			map.put(list.get(i).getMonths(),list.get(i).getNum());
		}
		List<String> timeList=getMonthBetween(begin,end);
		for (int i = 1; i <timeList.size()-1 ; i++) {
			if (!map.containsKey(timeList.get(i))){
				map.put(timeList.get(i),"0");
			}
		}
		List<MonthVo> mvList=new ArrayList<>();
		MonthVo mv=null;
		for (Map.Entry<String, String> entry : map.entrySet()) {
			mv=new MonthVo();
			mv.setMonths(entry.getKey());
			mv.setNum(entry.getValue());
			mvList.add(mv);
		}
		mvList.sort((h1, h2) -> h1.getMonths().compareTo(h2.getMonths()));
		return mvList;
	}
	/*查询本日的开始和结束时间段*/
	public static List<String> theSameDay() {
		 Calendar todayStart = Calendar.getInstance();
	       todayStart.set(Calendar.HOUR_OF_DAY, 0);
	       todayStart.set(Calendar.MINUTE, 0);
	       todayStart.set(Calendar.SECOND, 0);
	       Calendar todayEnd = Calendar.getInstance();
	       todayEnd.set(Calendar.HOUR_OF_DAY, 23);
	       todayEnd.set(Calendar.MINUTE, 59);
	       todayEnd.set(Calendar.SECOND, 59);
	       List<String> list=new ArrayList<String>();
	       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	       list.add(sdf.format(todayStart.getTime()));
	       list.add(sdf.format(todayEnd.getTime()));
	       return list;
	}

	/*根据一个时间查询本日放药开始和结束时间段*/
	public static List<String> todayPutMedicTime(Date time) {
		 Calendar todayStart = Calendar.getInstance();
		 todayStart.setTime(time);
	       todayStart.set(Calendar.HOUR_OF_DAY, -6);
	       Calendar todayEnd = Calendar.getInstance();
	       todayEnd.setTime(time);
	       todayEnd.set(Calendar.HOUR_OF_DAY, 18);
	       List<String> list=new ArrayList<String>();
	       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");
	       list.add(sdf.format(todayStart.getTime()));
	       list.add(sdf.format(todayEnd.getTime()));
	       return list;
	}
	/*查询本日装药的三个时间段*/
	public static List<String> theThreeTimeQuantum() {
		Calendar time0 = Calendar.getInstance();
		time0.set(Calendar.HOUR_OF_DAY, -6);
		time0.set(Calendar.MINUTE, 0);
		time0.set(Calendar.SECOND, 0);
		Calendar time1 = Calendar.getInstance();
		time1.set(Calendar.HOUR_OF_DAY, 8);
		time1.set(Calendar.MINUTE, 0);
		time1.set(Calendar.SECOND, 0);
		Calendar time2 = Calendar.getInstance();
		time2.set(Calendar.HOUR_OF_DAY, 12);
		time2.set(Calendar.MINUTE, 0);
		time2.set(Calendar.SECOND, 0);
		Calendar time3 = Calendar.getInstance();
		time3.set(Calendar.HOUR_OF_DAY, 18);
		time3.set(Calendar.MINUTE, 0);
		time3.set(Calendar.SECOND, 0);
		List<String> list=new ArrayList<String>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		list.add(sdf.format(time0.getTime()));
		list.add(sdf.format(time1.getTime()));
		list.add(sdf.format(time2.getTime()));
		list.add(sdf.format(time3.getTime()));
		return list;
	}
	/*查询本日的时间段*/
	public static List<String> theSameDay(String time) {
		String time2=time+" 00:00:00";
		Long startHs=getTimeChange(time2);
		Long endhs=startHs+86400000l;
		List<String> list=new ArrayList<String>();
		list.add(getSecondChange(startHs));
		list.add(getSecondChange(endhs));
		return list;
	}
	 /*查询第n个上周*/
	 public static List<String> lastNextWeek(int num) {
		 Calendar calendar = Calendar.getInstance();
		 int day = calendar.get(Calendar.DAY_OF_WEEK)-2;
		 if(day==-2) {
			 day=6;
		 }
		 Date date = new Date();
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		 Long currTime=getTimeChange(sdf.format(date));  //获得今天0点毫秒数
		 Long beginHs=currTime-(day*86400000l);
		 Long endHs=currTime+((7-day)*86400000l);
		 if(num!=0) {
			 beginHs=beginHs+(num*7*86400000l);
			 endHs=endHs+(num*7*86400000l);
		 }
		 String beginTime=getSecondChange(beginHs);
		 String endTime=getSecondChange(endHs);
		 List<String> list=new ArrayList<String>();
		 list.add(beginTime);
		 list.add(endTime);
		 return list;
	 }

	 
	 /*查询第n个上月*/
	 public static List<String> lastNextMonth(int num) {
		 Calendar c = Calendar.getInstance();
		 Calendar ca = Calendar.getInstance();
			c.add(Calendar.MONTH, num); //获取当前月第一天
			ca.add(Calendar.MONTH, num);
			ca.set(Calendar.DAY_OF_MONTH, 31);
	        c.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
	        c.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
	        c.set(Calendar.MINUTE, 0); //将分钟至0
	        c.set(Calendar.SECOND,0); //将秒至0
	        c.set(Calendar.MILLISECOND, 0); //将毫秒至0
	        ca.set(Calendar.HOUR_OF_DAY, 23); //将小时至23
	        ca.set(Calendar.MINUTE, 59); //将分钟至59
	        ca.set(Calendar.SECOND,59); //将秒至59
	        ca.set(Calendar.MILLISECOND, 59); //将毫秒至59
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String lastTime=sdf.format(c.getTime());
	        String endTime=sdf.format(ca.getTime());
	        List<String> list=new ArrayList<String>();
			 list.add(lastTime);
			 list.add(endTime);
			 return list;
	 }
	public static Date toDate(String dateStr) {
		try {
			return new SimpleDateFormat(df_Str).parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date toDate(String dateStr, String format) {
		try {
			return new SimpleDateFormat(format).parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date toYMDDate(String dateStr) {
		try {
			return new SimpleDateFormat(YYYY_MM_DD_Str).parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String toStr(Date date) {
		try {
			return new SimpleDateFormat(df_Str).format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return date == null ? "" : date.toString();
		}
	}

	public static String toStr(Object date) {
		try {
			return new SimpleDateFormat(df_Str).format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return date == null ? "" : date.toString();
		}
	}

	 public static long dateToTimeStamp(Date date) {
		    Timestamp timestamp = new Timestamp(date.getTime());
		    return timestamp.getTime();
	}


	/**
	 * 验证时间字符串格式输入是否正确
	 * @param timeStr
	 * @return
	 */
	public static boolean valiDateTimeWithLongFormat(String timeStr) {
		boolean convertSuccess=true;
		// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			format.setLenient(false);
			format.parse(timeStr);
		} catch (ParseException e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess=false;
		}
		return convertSuccess;
	}
	/**
	 * 验证时间字符串格式输入是否正确
	 * 	 * @param timeStr
	 * @return
	 */
	public static boolean valiDateTimeWithLongFormat1(String timeStr) {
		boolean convertSuccess=true;
		// 指定日期格式为四位年/两位月份，注意yyyy/MM区分大小写；
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		try {
			// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			format.setLenient(false);
			format.parse(timeStr);
		} catch (ParseException e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess=false;
		}
		return convertSuccess;
	}
	 
	/**
	 * 增加时间 （小时）
	 * 
	 * @param startDate
	 * @param hour
	 * @return
	 */
	public static Date addHour(Date startDate, int hour) {
		String format = new SimpleDateFormat(df_Str).format(startDate);
		Date date = null;
		try {
			date = new SimpleDateFormat(df_Str).parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.HOUR_OF_DAY, hour);
		return ca.getTime();
	}

	/**
	 * 转化成上午、下午
	 * 
	 * @param date
	 * @return
	 */
	public static String toAPM(Date date) {
		return new SimpleDateFormat(df_apm_Str).format(date);
	}

	public static String toAPM(String date) {
		try {
			return new SimpleDateFormat(df_apm_Str).format(new SimpleDateFormat(df_Str).parse(date));
		} catch (ParseException e) {
			return date;
		}
	}

	public static String format(Date date, String format) {
		if (date == null || "".equals(format) || format == null) {
			return null;
		}
		return new SimpleDateFormat(format).format(date);
	}

	public static Date parse(String date, String format) {
		if (date == null || "".equals(format) || format == null) {
			return null;
		}
		try {
			return new SimpleDateFormat(format).parse(date);
		} catch (ParseException e) {
			
		}
		return null;
	}

	public static long dateToTimestamp(String dateStr) {
		Date d = toDate(dateStr);
		if (null != d) {
			return d.getTime();
		}
		return 0;
	}
	public static String unixTimestampToDateStr(long timestamp) {
		return timestampToDateStr(timestamp, true);
	}

	public static String timestampToDateStr(long timestamp, boolean isUnix) {
		if (timestamp == 0)
			return null;
		if (isUnix)
			return new SimpleDateFormat(df_Str).format(timestamp * 1000);
		return new SimpleDateFormat(df_Str).format(timestamp);
	}
	public static long getUnixNowTimestamp(){
		return (getNowTimestamp()/1000);
	}
	public static long getNowTimestamp(){
		return System.currentTimeMillis();
	}

	public static String getNowToStrYYYY_MM_DD() {
		return new SimpleDateFormat(YYYY_MM_DD_Str).format(new Date());
	}
	public  static String getTime(){
		Date data=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(data);
	}
	// day = -1 表示上一天
	public static long lastDays(int day) {
		Calendar c = setCalendar();
		c.add(Calendar.DATE, day);
		Date d = c.getTime();
		return d.getTime();
	}

	// month = -1 表示过去一个月
	public static long lastMonth(int month) {
		Calendar c = setCalendar();
		c.add(Calendar.MONTH, month);
		Date m = c.getTime();
		return m.getTime();
	}

	public static long lastYear(int year) {
		// 过去一年
		Calendar c = setCalendar();
		c.add(Calendar.YEAR, year);
		Date y = c.getTime();
		return y.getTime();
	}

	private static Calendar setCalendar() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return c;
	}

	//一天开始  0点0时0分
	public static long getStartTime() {  
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();  
    } 
	//一天结束  23点59时59分
	public static long getnowEndTime() {  
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime().getTime();  
    }  
	
	/**
	 * 当前时间是否是今天的?到?之间,例:"19:20:00","20:43:00"(不能夸天)
	 * @param stime
	 * @param etime
	 * @return
	 */
	public  static boolean isRunTime(String stime, String etime) {
		String nowString = format(new Date(System.currentTimeMillis()), "HH:mm:ss");
		String startTime = stime;
		String overTime = etime;

		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
		Date dtStart = null;
		Date dtOver = null;
		Date now = null;
		try {
			dtStart = sdf.parse(startTime);
			dtOver = sdf.parse(overTime);
			now = sdf.parse(nowString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		boolean isNullIssueTime = (now.getTime() >= dtStart.getTime())
				&& (now.getTime() <= dtOver.getTime());
		return isNullIssueTime;
	}
	
	//字符串转毫秒数
	public static Long getTimeChange(String sdate){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long time=1l;
		try {
			time = simpleDateFormat.parse(sdate).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;
	}
	//毫秒数转时间
	public static String getSecondChange(Long millionSeconds){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(millionSeconds); 
		Date date = c.getTime();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	//字符串毫秒数转时间类型
	public static Date StringToDate(String longTime){
		Date date = new Date();
		date.setTime(Long.valueOf(longTime));
		return date;
	}

	
	/**
	 * @Desc 字符串转date
	 * @param date
	 * @return
	 * @throws ParseException
	 * @author Lhl
	 * @Date 2019年5月23日 下午3:34:25
	 */
	public static Date stringToDate(String date) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(date);
	}
	
	/**
	 * @Desc 判断两个日期是否相等
	 * @param d1
	 * @param d2
	 * @return
	 * @author Lhl
	 * @Date 2019年5月23日 下午5:15:35
	 */
	public static boolean sameDate(Date d1, Date d2){
		SimpleDateFormat fmt = new SimpleDateFormat("MMdd");
		//fmt.setTimeZone(new TimeZone()); // 如果需要设置时间区域，可以在这里设置
		return fmt.format(d1).equals(fmt.format(d2));
	}
	
 
	public static String getMiniteTimeNow(){
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
		String time=sdf.format(date);
		return time;
	}
	
	
	
}
